package com.ruoyi.eqmonitor.controller;

import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "接收设备数据")
@Deprecated
public class ReceiveDeviceDataController {

    private final Logger logger = LoggerFactory.getLogger(ReceiveDeviceDataController.class);

    /**
     * 弃用 原本是给一期设备准备的, 但是他们不用这种方式了
     */
    @ApiOperation(value = "接收设备数据")
    @RequestMapping("/receive")
    public R<Boolean> receivePostBody(HttpServletRequest request, @RequestParam(required = false) Map<String, Object> params, @RequestBody(required = false) String message) {
        String method = request.getMethod();
        Enumeration<String> headerNames = request.getHeaderNames();
        Map<String, Object> headers = new HashMap<>();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String headerName = headerNames.nextElement();
                headers.put(headerName, request.getHeader(headerName));
            }
        }
        String contentType = request.getContentType();
        String protocol = request.getProtocol();
        String requestInfo = "{ " +
                "method: " + method + ", " +
                "headers: " + headers + ", " +
                "contentType: " + contentType + ", " +
                "protocol: " + protocol +
                " }";
        logger.info(
                "接收设备数据: \n" +
                "请求方式: " + requestInfo + "\n" +
                "params: " + params + "\n"+
                "body: " + message);
        return R.ok();

    }

}
